package com.dgut.uhrs.controller;
import com.dgut.uhrs.bean.Key;
import com.dgut.uhrs.bean.Record;
import com.dgut.uhrs.service.RecordService;
import com.dgut.uhrs.service.UserService;
import com.dgut.uhrs.util.JWTUtil;
import com.dgut.uhrs.vo.DataVO;
import com.dgut.uhrs.vo.RecordVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user/uploadRecord")
    @ResponseBody
    public Boolean addRecord(Record record , HttpServletRequest request){
        String token = request.getHeader("token");
        if (userService.checkToken(token)) {
            Record r = recordService.findRecordByUserIdAndDate(record.getUid(),record.getDate());
            if(r!=null){
                return false;
            }
            else{
                return recordService.addRecord(record);
            }
        }
        else {
            return false;
        }
    }


    @RequestMapping(value = "/user/hasRecord")
    @ResponseBody
    public Boolean hasRecord(@Param("uid")String uid, @Param("date")String date, HttpServletRequest request){
        String token = request.getHeader("token");
        if (userService.checkToken(token)) {
            Record r = recordService.findRecordByUserIdAndDate(uid,date);
            if(r!=null){
                return true;
            }
            else{
                return false;
            }
        }
        else {
            return false;
        }
    }

    @RequestMapping(value = "/user/findTodayAbnormalRecord")
    @ResponseBody
    public List<Record> findTodayAbnormalRecordWithOnePage(HttpServletRequest request){
        String token = request.getHeader("token");
        if (userService.checkToken(token)) {
            return recordService.findTodayAbnormalRecordWithOnePage();
        }
        else {
            return null;
        }

    }

    @RequestMapping(value = "/admin/toShowAllRecord")
    public String toShowAllRecord(){
        return "allRecord";
    }
    @RequestMapping(value = "/admin/showAllRecord")
    @ResponseBody
    public DataVO<RecordVO> showAllRecord(Key key, HttpServletRequest request){
        key = setKey(key,request);
        return recordService.showAllRecord(key);
    }



    @RequestMapping(value = "/admin/toTodayRecord")
    public String toTodayRecord(){
        return "recordOfTheDay";
    }
    @RequestMapping(value = "/admin/findTodayRecord")
    @ResponseBody
    public DataVO<RecordVO> findTodayRecord(Key key, HttpServletRequest request){
        key = setKey(key,request);
        return recordService.findTodayRecord(key);
    }

    @RequestMapping(value = "/admin/toAbnormalRecord")
    public String toAbnormalRecord(){
        return "abnormalRecord";
    }
    @RequestMapping(value = "/admin/findAbnormalRecord")
    @ResponseBody
    public DataVO<RecordVO> findAbnormalRecord(Key key, HttpServletRequest request){
        key = setKey(key,request);
        return recordService.findAbnormalRecord(key);
    }


    @RequestMapping(value = "/admin/toTodayAbnormalRecord")
    public String toTodayAbnormalRecord(){
        return "todayAbnormalRecord";
    }
    @RequestMapping(value = "/admin/findTodayAbnormalRecord")
    @ResponseBody
    public DataVO<RecordVO> findTodayAbnormalRecord(Key key,HttpServletRequest request){
        key = setKey(key,request);
        return recordService.findTodayAbnormalRecord(key);
    }

    @RequestMapping(value = "/admin/editRecord")
    @ResponseBody
    public Boolean editRecord(Key key){
        return recordService.updateRecord(key);
    }

    @RequestMapping(value = "/admin/deleteRecord")
    @ResponseBody
    public void deleteRecord(Key key){
        recordService.deleteRecord(key);
    }


    public Key setKey(Key key, HttpServletRequest request){
        key.setId(request.getParameter("key[id]"));
        key.setName(request.getParameter("key[name]"));
        key.setDate(request.getParameter("key[date]"));
        key.setAddress(request.getParameter("key[address]"));
        key.setClas(request.getParameter("key[clas]"));
        key.setLimit(Integer.parseInt(request.getParameter("limit")));
        key.setSchool(request.getParameter("key[school]"));
        key.setPhone(request.getParameter("key[phone]"));
        key.setPage((key.getPage()-1)* key.getLimit());
        key.setToken(request.getParameter("key[token]"));
        return key;
    }
}
